package com.ybt.heartratem.common.utils;

import android.graphics.Bitmap;

import com.caysn.autoreplyprint.AutoReplyPrint;
import com.sun.jna.Pointer;

public class PrintBitmapUtilsV2 {

    private static Pointer hPrinter = Pointer.NULL;

    public static boolean printBitmap(Bitmap bitmap) {
        boolean result = false;

        //如果端口未打开，先打开端口。
        if (!AutoReplyPrint.INSTANCE.CP_Port_IsConnectionValid(hPrinter)) {
            AutoReplyPrint.INSTANCE.CP_Port_Close(hPrinter);

            if (!AutoReplyPrint.INSTANCE.CP_Port_IsOpened(hPrinter))
                hPrinter = AutoReplyPrint.INSTANCE.CP_Port_OpenUsb("VID:0x4B43,PID:0x3538", 0);
            if (!AutoReplyPrint.INSTANCE.CP_Port_IsOpened(hPrinter))
                hPrinter = AutoReplyPrint.INSTANCE.CP_Port_OpenUsb("VID:0x4B43,PID:0x3830", 0);
            if (!AutoReplyPrint.INSTANCE.CP_Port_IsOpened(hPrinter))
                hPrinter = AutoReplyPrint.INSTANCE.CP_Port_OpenUsb("VID:0x0FE6,PID:0x811E", 0);
            if (!AutoReplyPrint.INSTANCE.CP_Port_IsOpened(hPrinter))
                hPrinter = AutoReplyPrint.INSTANCE.CP_Port_OpenUsb("VID:0x067B,PID:0x2303", 0);
        }

        //如果端口已打开，则发送数据
        if (AutoReplyPrint.INSTANCE.CP_Port_IsOpened(hPrinter)) {
            AutoReplyPrint.INSTANCE.CP_Label_BackPaperToPrintPosition(hPrinter);
            AutoReplyPrint.CP_Pos_PrintRasterImageFromData_Helper.PrintRasterImageFromBitmap(hPrinter, bitmap.getWidth(), bitmap.getHeight(), bitmap, AutoReplyPrint.CP_ImageBinarizationMethod_Thresholding, 0);
            AutoReplyPrint.INSTANCE.CP_Label_FeedPaperToTearPosition(hPrinter);

            result = AutoReplyPrint.INSTANCE.CP_Port_IsOpened(hPrinter);
        }

        return result;
    }
}
